<?php

require_once("field.php");
require_once("iobserver.php");

class entity implements iobserver {
	
	//private $state;
	public $id;
	public $name;
	private $fields = array();
	private $items = array();
	
/*
	function __construct() {
		$field = new field();
		$field->name = "uuid";
		$this->addfield($field);
	}
*/
	
	function addfield($field = null) {
		if (!$field) {
			$field = new field();
		}
		$field->addObserver($this,1);
		$this->fields[] = $field;
		return $field;
	}
	
	function fieldbyname($name){
		foreach ($this->fields as $field) {
			if ($name == $field->name) {
				return $field;
			}
		}
	}
	
	function getallfields() {
		return $this->fields;
	}
	
	function observable_before_changed($sender, &$value) {
		foreach ($this->fields as $field) {
			${$field->name} = $field;
		}
		eval($sender->beforechange);
	}
	
	function observable_after_changed($sender, &$value) {
		foreach ($this->fields as $field) {
			${$field->name} = $field;
		}
		eval($sender->afterchange);
	}
	
	function save() {
		
	}
}
?>

